package com.owen.he.jee.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.owen.he.jee.service.IndexService;
import com.owen.he.jee.utils.JsonMapper;
import com.owen.he.jee.vo.excel.ImportExcelResult;


/**
 * 主页接口
 * @author owen.he
 *
 */
@Controller
@RequestMapping("index")
public class IndexController {
	
	@Autowired
	IndexService indexService;
	
	/**
	 * @param resquest
	 * @return
	 */
	@RequestMapping
	@ResponseBody
	public String index(HttpServletRequest resquest) {
		return "OK中文测试等等 爱上的擦拭的擦拭的参数";
	}
	
	@RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
	@ResponseBody
	public String uploader(MultipartHttpServletRequest request) {
		String type = request.getParameter("type");
		MultipartFile file = request.getFile("file");
		Map<String, Object> msg = new HashMap<>();
		ImportExcelResult importExcel = indexService.importExcel(file);
		msg.put("successNum", importExcel.getSuccessRecord());
		msg.put("failNum", importExcel.getFailRecord());
		msg.put("message", importExcel.getMesageResult());
		String json = JsonMapper.getInstance().toJson(msg);
		return json;
	}
	
	@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
	public String toDownload(HttpServletResponse response, HttpServletRequest request) {
		try {
			//indexService.downloadFile(response, request);
			indexService.downloadSteam(response, request);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}
}
